package com.abaaba.project2.service.serviceImpl;

import com.abaaba.project2.bean.*;
import com.abaaba.project2.bean.bo.OrderRefundBo;
import com.abaaba.project2.bean.bo.OrderShipBo;
import com.abaaba.project2.bean.data.BaseData;
import com.abaaba.project2.bean.vo.AdminGoodsStatisticsVO;
import com.abaaba.project2.bean.vo.AdminOrderStatisticsVO;
import com.abaaba.project2.bean.vo.AdminUserStatisticsVO;
import com.abaaba.project2.bean.vo.DetailVo;
import com.abaaba.project2.mapper.OrderGoodsMapper;
import com.abaaba.project2.mapper.OrderMapper;
import com.abaaba.project2.mapper.UserMapper;
import com.abaaba.project2.service.AdminOrderService;
import com.abaaba.project2.util.DateToStringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.System;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AdminOrderServiceImpl implements AdminOrderService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderGoodsMapper orderGoodsMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public BaseData<Order> queryOrder(Integer page, Integer limit, Short[] orderStatusArray, String sort, String order, Integer userId, String orderSn) {
        PageHelper.startPage(page, 20);

        OrderExample orderExample = new OrderExample();
        OrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (orderStatusArray != null) {
            List<Short> shortList = new ArrayList<>();
            for (Short aShort : orderStatusArray) {
                shortList.add(aShort);
            }
            if (!shortList.isEmpty()) {
                criteria.andOrderStatusIn(shortList);
            }
        }
        if (userId != null && !("".equals(String.valueOf(userId)))) {
            criteria.andUserIdEqualTo(userId);
        }
        if (orderSn != null && !("".equals(orderSn))) {
            criteria.andOrderSnEqualTo(orderSn);
        }

        orderExample.setOrderByClause(sort + " " + order);
        List<Order> orders = orderMapper.selectByExample(orderExample);
        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);


        return BaseData.ok(orders,orderPageInfo.getTotal());
    }

    @Override
    public DetailVo queryDetail(Integer id) {
        DetailVo detailVo = new DetailVo();
        Order order = orderMapper.selectByPrimaryKey(id);
        detailVo.setOrder(order);

        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        OrderGoodsExample.Criteria criteria = orderGoodsExample.createCriteria();
        criteria.andOrderIdEqualTo(id);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);
        detailVo.setOrderGoods(orderGoods);

        User user = userMapper.selectByPrimaryKey(detailVo.getOrder().getUserId());
        DetailVo.UserBean userBean = new DetailVo.UserBean();
        detailVo.setUser(userBean);
        detailVo.getUser().setNickname(user.getNickname());
        detailVo.getUser().setAvatar(user.getAvatar());
        return detailVo;
    }

    @Override
    public Integer shipOrderById(OrderShipBo orderShipBo) {
        Order order = new Order();
        order.setId(orderShipBo.getOrderId());
        order.setShipChannel(orderShipBo.getShipChannel());
        order.setShipSn(orderShipBo.getShipSn());
        order.setShipTime(new Date(java.lang.System.currentTimeMillis()));
        order.setUpdateTime(order.getShipTime());
        order.setOrderStatus(((short) 301));
        return orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public Integer refundOrderById(OrderRefundBo orderRefundBo) {
        Order order = new Order();
        order.setId(orderRefundBo.getOrderId());
        order.setOrderStatus(((short) 203));
        order.setUpdateTime(new Date(java.lang.System.currentTimeMillis()));
        order.setEndTime(order.getUpdateTime());
        return orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public AdminOrderStatisticsVO OrderNumberSearchAllCount() {
        /*根据时间来查询订单总额*/
        List<AdminUserStatisticsVO.DataBean.RowsBean> rowList = new ArrayList<>();
        AdminUserStatisticsVO.DataBean.RowsBean rowsBean = new AdminUserStatisticsVO.DataBean.RowsBean();

        List<String> columnsList = new ArrayList<>();
        columnsList.add("day");
        columnsList.add("orders");
        columnsList.add("customers");
        columnsList.add("amount");
        columnsList.add("pcr");

        /*这里应该是有所有的日期信息*/
        List<CountByDateTime> orderList = orderMapper.countByDate();
        /*数据取出来了，需要将data转换为String，我觉得不需要*/
        List<AdminOrderStatisticsVO.DataBean.RowsBean> rowsBeanList = new ArrayList<>();
        for (CountByDateTime countByDateTime : orderList) {

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String sratOrder = simpleDateFormat.format(countByDateTime.getTime());
            CountByDateTimeAndAll countByDateTimeAndAll = orderMapper.countByDateTimeAndAll(countByDateTime.getTime());
            AdminOrderStatisticsVO.DataBean.RowsBean rowsBean1 = new AdminOrderStatisticsVO.DataBean.RowsBean(countByDateTimeAndAll.getAmount(),countByDateTimeAndAll.getOrders(),countByDateTimeAndAll.getCustomers(),sratOrder,countByDateTimeAndAll.getPcr());
            rowsBeanList.add(rowsBean1);
        }
        AdminOrderStatisticsVO.DataBean dataBean = new AdminOrderStatisticsVO.DataBean(columnsList,rowsBeanList);
        AdminOrderStatisticsVO adminOrderStatisticsVO = new AdminOrderStatisticsVO();

        adminOrderStatisticsVO.setData(dataBean);
        adminOrderStatisticsVO.setErrmsg("成功");
        adminOrderStatisticsVO.setErrno(0);

        return adminOrderStatisticsVO;
    }

    @Override
    public AdminGoodsStatisticsVO goodsNumberSearchAllCount() {

        List<String> columnsList = new ArrayList<>();
        columnsList.add("day");
        columnsList.add("orders");
        columnsList.add("products");
        columnsList.add("amount");

        /*这里应该是有所有的日期信息*/
        List<CountByDateTime> orderList = orderMapper.countByDateOfOrderGoods();
        List<AdminGoodsStatisticsVO.DataBean.RowsBean> rowsBeanList = new ArrayList<>();
        for (CountByDateTime countByDateTime : orderList) {

            SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd");
            String dateString=formatter.format(countByDateTime.getTime());

            CountByDateTimeOfGoods countByDateTimeOfGoods = orderMapper.countByDateTimeOfOrderGoods(countByDateTime.getTime());
            AdminGoodsStatisticsVO.DataBean.RowsBean rowsBean = new AdminGoodsStatisticsVO.DataBean.RowsBean(countByDateTimeOfGoods.getAmount(),countByDateTimeOfGoods.getOrders(),dateString,countByDateTimeOfGoods.getProducts());
            rowsBeanList.add(rowsBean);
        }
        AdminGoodsStatisticsVO.DataBean dataBean = new AdminGoodsStatisticsVO.DataBean(columnsList,rowsBeanList);

        AdminGoodsStatisticsVO adminGoodsStatisticsVO = new AdminGoodsStatisticsVO();
        adminGoodsStatisticsVO.setData(dataBean);
        adminGoodsStatisticsVO.setErrmsg("成功");
        adminGoodsStatisticsVO.setErrno(0);
        return adminGoodsStatisticsVO;
    }

    @Override
    public Long countSearchOrderAll() {
        OrderExample orderExample = new OrderExample();
        // 构造查询条件
        OrderExample.Criteria criteria = orderExample.createCriteria();
        Long code = orderMapper.countByExample(orderExample);
        return code;
    }


}
